from multiprocessing import Process, Array, Value, Lock, Queue
import time

"""
课堂实例自己要实现一遍
把概念性的内容熟记和理解.
"""


# 自定义Response类
class Response:
    status_code = 200


# 基础方式
result = []


def sim_request(index, result):
    response = Response()
    time.sleep(0.2)
    print(f'current response: {response.status_code}')
    print(f'result id: {id(result)}')
    result.append(response.status_code)


# 第一参数 i :(int)  u: (str)  第二参数  长度限定
result1 = Array('i', 100)


def sim_request1(index, result1):
    response = Response()
    time.sleep(0.2)
    print(f'current response: {response.status_code}')
    print(f'result id: {id(result1)}')
    result1[index] = response.status_code


# 文件共享会丢失写入数据，由此引入文件互斥锁
# f = open('result.log', 'a', encoding='utf-8')


def sim_file(index):
    response = Response()
    time.sleep(0.2)
    print(f'index: {index}, current response: {response.status_code}')
    with open('result.log', 'a', encoding='utf-8') as f:
        f.write(f'index: {index}, current response: {response.status_code}\n')


# 文件应用：互斥锁
def file_lock(index, lock):
    with lock:
        response = Response()
        time.sleep(0.2)
        print(f'index: {index}, current response: {response.status_code}')
        with open('result.log', 'a', encoding='utf-8') as f:
            f.write(f'index: {index}, current response: {response.status_code}\n')


# 队列
queue_conn = Queue(maxsize=50)


def get_queue(index, queue_conn):
    response = Response()
    time.sleep(0.2)
    print(f'index: {index}, current response: {response.status_code}')
    queue_conn.put({index: response.status_code}, timeout=1)


if __name__ == '__main__':
    p_array = []
    for index in range(10):
        p = Process(target=sim_request, args=(index, result))
        p.start()
        p_array.append(p)
    [p.join() for p in p_array]
    print('-----------------sim_request: done----------------------')

    """数组Array实例"""
    p_array = []
    for index in range(10):
        p = Process(target=sim_request1, args=(index, result1))
        p.start()
        p_array.append(p)
    [p.join() for p in p_array]
    print('-------------------sim_request1 done--------------------')

    """File文件的用法"""
    p_array = []
    for index in range(10):
        p = Process(target=sim_file, args=(index,))
        p.start()
        p_array.append(p)
    [p.join() for p in p_array]
    print('-------------------sim_file.done--------------------')

    """File文件互斥锁使用"""
    p_array = []
    lock = Lock()
    for index in range(10):
        p = Process(target=file_lock, args=(index, lock))
        p.start()
        p_array.append(p)
    [p.join() for p in p_array]
    print('-------------------file_lock.done--------------------')

    """Queue使用方法"""
    p_array = []
    for index in range(10):
        p = Process(target=get_queue, args=(index, queue_conn))
        p.start()
        p_array.append(p)
    [p.join() for p in p_array]
    print('queue队列：')
    for i in range(10):
        print(queue_conn.get())
    print('-------------------get_queue.done--------------------')
